package com.bhs.sansong.ui.other;

import android.graphics.Bitmap;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bhs.sansong.R;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.utils.LogUtils;

/**
 * 外部链接
 */
public class WebViewActivity extends BaseActivity {

    private WebView mWebView;
    private ProgressBar mProgressBar;
    private String url;


    @Override
    public int initLayout() {
        return R.layout.activity_web_view;
    }

    @Override
    public void initView() {
        url = getIntent().getStringExtra("url");
        mWebView = findViewById(R.id.webView);
        mProgressBar = findViewById(R.id.ProgressBar);
        mWebView.loadUrl(url);
        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText(getIntent().getStringExtra("title"));
        mWebView.reload();//每次都重新加载网页
        LogUtils.d(url);
    }

    @Override
    public void initData() {
        //设置支持js否则有些网页无法打开
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.setWebChromeClient(new MyWebChromeClient());

    }


    class MyWebChromeClient extends WebChromeClient {
        //监听网页进度 newProgress进度值在0-100
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            mProgressBar.setProgress(newProgress);
            if (newProgress > 95) {
                mProgressBar.setVisibility(View.GONE);
            }
        }

        //设置Activity的标题与 网页的标题一致
        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            setTitle(title);
        }
    }


    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }

    }
}